﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using Tools;

namespace WPF数据采集系统.Model
{
    public class ListBoxHelper
    {
        public static object obj = false;
        #region WPF listbox修改新增项颜色
        public static void listBoxAdd(ObservableCollection<string> listBox, string content, bool SaveOrNot=false)
        {
            lock(obj)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    SynchronizationContext.SetSynchronizationContext(new
                        DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                    SynchronizationContext.Current.Post(pl =>
                    {
                        int count = listBox.Count;
                        String Text = DateTime.Now.ToLocalTime().ToString() + " " + content;
                        string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\LogFile";
                        string FileName = DateTime.Now.ToString("yyyyMMdd_") + "log.txt";
                        string PathAndFileName = path + "\\" + FileName;
                        if (SaveOrNot)
                        {
                           LogHelper.WriteErrorLog(Text, PathAndFileName);
                        }
                        if (count == 20)
                        {
                            listBox.RemoveAt(19);
                        }
                        listBox.Insert(0, Text);
                    }, null);
                });
            }

        }
        public static void RotateListSet(int index,int[] Monitor)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {
                    RotateModel rm = new RotateModel();
                    rm.RowNumber = Monitor[index];
                    rm.RotateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    MainWindow.mvm.RotateList[Monitor[index]] = rm;
                }, null);
            });
        }
        #endregion
    }
}
